﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MPMovieCatalog.Configuration.Sections;
using MPMovieCatalog.Util;

namespace MPMovieCatalog.Configuration
{
    public partial class ConfigurationForm : Form
    {
        private Dictionary<string, ConfigurationFormSection> m_sections = new Dictionary<string, ConfigurationFormSection>();

        private ConfigurationWrapper m_configurationWrapper = new ConfigurationWrapper();

        public ConfigurationForm()
            : this(new ConfigurationFormProperties(true))
        {
        }

        public ConfigurationForm(ConfigurationFormProperties properties)
        {
            InitializeComponent();
            UpdateUI();

            if (properties.ShowConnectionSection)
            {
                AddSection(new ConnectionSectionForm(m_configurationWrapper.ConnectionSection));
            }
            if (properties.ShowScriptSection)
            {
                AddSection(new ScriptSectionForm(m_configurationWrapper.ScriptSection));
            }
        }

        public void SaveSettings()
        {
            m_configurationWrapper.SaveConfiguration();
        }

        private void sectionTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (sectionTreeView.SelectedNode != null && sectionTreeView.SelectedNode.Text != string.Empty)
            {
                ConfigurationFormSection section = m_sections[sectionTreeView.SelectedNode.Text];
                panel.Controls.Clear();
                panel.Controls.Add(section);
            }
        }

        private void AddSection(ConfigurationFormSection section)
        {
            m_sections.Add(section.Text, section);
            sectionTreeView.Nodes.Add(section.Text);
        }

        private void buttonPanel_Paint(object sender, PaintEventArgs e)
        {
            ControlPaint.DrawBorder3D(e.Graphics, this.buttonPanel.ClientRectangle, Border3DStyle.Etched, Border3DSide.Top);
        }

        private void buttonPanel_Resize(object sender, EventArgs e)
        {
            Invalidate();
        }

        private void UpdateUI()
        {
            this.Text = ResourceUtils.GetString("options", 0);
            okButton.Text = ResourceUtils.GetString("options", 1);
            cancelButton.Text = ResourceUtils.GetString("options", 2);
        }
    }

    public class ConfigurationFormProperties
    {
        public ConfigurationFormProperties(bool showByDefault)
        {
            ShowConnectionSection = showByDefault;
            ShowScriptSection = showByDefault;
        }

        public bool ShowConnectionSection { get; set; }
        public bool ShowScriptSection { get; set; }
    }
}
